<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Notification extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'type',
        'notifiable_type',
        'notifiable_id',
        'from_user_id',
        'title',
        'content',
        'data',
        'is_read',
        'read_at',
    ];

    protected $casts = [
        'data' => 'array',
        'is_read' => 'boolean',
        'read_at' => 'datetime',
    ];

    /**
     * 获取通知接收者
     */
    public function notifiable(): MorphTo
    {
        return $this->morphTo();
    }

    /**
     * 标记为已读
     */
    public function markAsRead()
    {
        if (is_null($this->read_at)) {
            $this->update(['read_at' => now()]);
        }
    }

    /**
     * 标记为未读
     */
    public function markAsUnread()
    {
        if (!is_null($this->read_at)) {
            $this->update(['read_at' => null]);
        }
    }

    /**
     * 判断是否已读
     */
    public function isRead(): bool
    {
        return !is_null($this->read_at);
    }

    /**
     * 判断是否未读
     */
    public function isUnread(): bool
    {
        return is_null($this->read_at);
    }

    /**
     * 作用域：未读通知
     */
    public function scopeUnread($query)
    {
        return $query->whereNull('read_at');
    }

    /**
     * 作用域：已读通知
     */
    public function scopeRead($query)
    {
        return $query->whereNotNull('read_at');
    }

    /**
     * 获取通知标题
     */
    public function getTitle(): string
    {
        $titles = [
            'follow' => '新的关注',
            'comment' => '新的评论',
            'like' => '收到点赞',
            'system' => '系统通知',
            'post_approved' => '文章审核通过',
            'post_rejected' => '文章审核未通过',
            'mention' => '有人提到了你',
            'reply' => '收到回复',
        ];

        return $titles[$this->type] ?? '新通知';
    }

    /**
     * 获取通知图标
     */
    public function getIcon(): string
    {
        $icons = [
            'follow' => 'user-plus',
            'comment' => 'message-circle',
            'like' => 'heart',
            'system' => 'bell',
            'post_approved' => 'check-circle',
            'post_rejected' => 'x-circle',
            'mention' => 'at-sign',
            'reply' => 'message-square',
        ];

        return $icons[$this->type] ?? 'bell';
    }

    /**
     * 获取通知颜色
     */
    public function getColor(): string
    {
        $colors = [
            'follow' => 'blue',
            'comment' => 'green',
            'like' => 'red',
            'system' => 'gray',
            'post_approved' => 'green',
            'post_rejected' => 'red',
            'mention' => 'purple',
            'reply' => 'indigo',
        ];

        return $colors[$this->type] ?? 'gray';
    }

    /**
     * 获取通知消息
     */
    public function getMessage(): string
    {
        $data = $this->data;

        switch ($this->type) {
            case 'follow':
                return "{$data['user_name']} 关注了你";

            case 'comment':
                return "{$data['user_name']} 评论了你的文章《{$data['post_title']}》";

            case 'like':
                return "{$data['user_name']} 赞了你的文章《{$data['post_title']}》";

            case 'post_approved':
                return "你的文章《{$data['post_title']}》已通过审核";

            case 'post_rejected':
                return "你的文章《{$data['post_title']}》审核未通过：{$data['reason']}";

            case 'mention':
                return "{$data['user_name']} 在评论中提到了你";

            case 'reply':
                return "{$data['user_name']} 回复了你的评论";

            case 'system':
                return $data['message'] ?? '系统通知';

            default:
                return '你有新的通知';
        }
    }

    /**
     * 获取通知链接
     */
    public function getUrl(): ?string
    {
        $data = $this->data;

        switch ($this->type) {
            case 'follow':
                return "/profile/{$data['user_username']}";

            case 'comment':
            case 'like':
            case 'mention':
            case 'reply':
                return "/posts/{$data['post_slug']}#comment-{$data['comment_id'] ?? ''}";

            case 'post_approved':
            case 'post_rejected':
                return "/profile/posts";

            default:
                return null;
        }
    }
}